package codingforgreat.lchot150;

import java.util.Arrays;

public class Lc056 {
    public int[][] merge(int[][] intervals) {
        if (intervals.length == 0) {
            return new int[0][0];
        }
        Arrays.sort(intervals,(a, b) ->a[0] - b[0]);
        int s = intervals[0][0];
        int e = intervals[0][1];
        int size = 0;
        for(int[] cur : intervals){
            if(cur[0] > e){
                intervals[size][0] = s;
                intervals[size++][1] = e;
                s = cur[0];
                e = cur[1];
            }else{
                e = Math.max(e,cur[1]);
            }
        }
        intervals[size][0] = s;
        intervals[size++][1] = e;
        return Arrays.copyOf(intervals,size);
    }
}
